package com.itheima.edu.info.manager.dao.impl;


import com.itheima.edu.info.manager.domain.Student;
public class StudentDao {

    //创建长度为5的学生数组对象容器   static修饰的成员可以被多个StudentDao对象调用
    private static Student[] students = new Student[5];

    public static Student[] findAllStudents() {
        return students;
    }

    public static boolean addStudent(Student student) {

        //找到学生对象储存的索引位置
        //假设没有这个位置 index = -1;
        int index = -1;
        //遍历学生数组
        for (int i = 0; i < students.length; i++) {
            Student student1 = students[i];
            //如果对象的地址是默认值null,表示这个索引位置可以储存
            if (student1 == null){
                //直接储存
                students[i] = student;
                return true;
            }
        }
        //不存在,返回false
        return false;
    }

    public boolean delectStudent(String id) {
        //遍历学生数组
        for (int i = 0; i < students.length; i++) {
            if (students[i] != null && students[i].getId().equals(id)) {
                students[i] = null;
                return true;
            }
        }
        //不存在返回false
        return false;
    }

    public Student selectStudent(String id) {
        //Student stu = new Student();
        int index = -1;
        //遍历学生数组
        for (int i = 0; i < students.length; i++) {
            if (students[i] != null && students[i].getId().equals(id)) {
                index = i;
                break;
            }
        }

        return students[index];
    }

    public Student updataStudent(Student student) {
        Student stu = new Student();
        int index = -1;
        //遍历学生数组
        for (int i = 0; i < students.length; i++) {
            if (students[i] != null && students[i].getId().equals(student.getId())) {
                students[i].setName(student.getName());
                students[i].setAge(student.getAge());
                students[i].setBirthday(student.getBirthday());
                index = i;
                break;
            }
        }

        return students[index];
    }
    //
}
